import sys, os, re, json

# Import lib
sys.path.append('lib')
#import urllib2
#import mechanize
from mechanize import Browser
from mechanize import HTTPRobotRulesProcessor

# Ignore robots.txt -------------------------------------------------

class AgnosticHTTPRobotRulesProcessor(HTTPRobotRulesProcessor):
	def http_request(self, request):
		# always return request
		return request

# -------------------------------------------------------------------
# Do some browsing

host = "monster.de"

br = Browser()
# Behave nasty ;-)
#br.set_handle_robots(AgnosticHTTPRobotRulesProcessor())
br.set_handle_robots(None)
#mech.set_debug_http(True)


#br.open("http://www.monster.de/")
##assert br.viewing_html()
##print br.title()
##response0 = br.response()
##print response0.geturl()
##print response0.info()  # headers
##print response0.read()  # body

# follow second link with element text matching regular expression
#response1 = br.follow_link(text_regex=r"cheese\s*shop", nr=1)
url = "http://jobsuche." + host + "/Search.aspx?q=php&cy=de&lid=163&re=130"
br.open(url)

response1 = br.response()
assert br.viewing_html()
print br.title()
#print response1.geturl()
#print response1.info()  # headers
#print response1.read()  # body

#def displaymatch(match):
#    if match is None:
#        return None
#    return '<Match: %r, groups=%r>' % (match.group(), match.groups())
#print displaymatch(re.search('JobSearchList = ([^\\n]*)',response1.read()))

joblistmatch = re.search('jsResults\.JobSearchList = ([^\\n]*)',response1.read())

if joblistmatch is None:
	print "No joblist was found!\n"
	exit()

jobliststr = joblistmatch.groups()[0].strip()
jobliststr = jobliststr.rstrip(';')
print jobliststr
jobs = json.loads(jobliststr)
print jobs
